﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class Photoalbum : BusinessObject
    {
        private System.Guid m_guidPhotoalbumId;
        private Guid m_guidHtmlPageId = Guid.Empty;
        private string m_sTitleTxt;
        private string m_sDescTxt;
        private System.DateTime m_dtCreateDttm;
        private System.DateTime m_dtUpdateDttm;
        private Guid m_guidLatestPhotoId = Guid.Empty;
        private Photo[] m_lstPhoto;
        private int m_iPhotoCnt;
        private bool m_bArchiveInd = false;
        private string m_sPageUrl;

        #region Public properties

        // tbl_photoalbum: photoalbum_id
        public virtual System.Guid PhotoalbumId
        {
            get
            {
                return this.m_guidPhotoalbumId;
            }
            set
            {
                if (!value.Equals(m_guidPhotoalbumId))
                {
                    this.m_guidPhotoalbumId = value;
                    this.SetDirty();
                }
            }
        }

        public virtual System.Guid HtmlPageId
        {
            get { return m_guidHtmlPageId; }
            set
            {
                if (!value.Equals(m_guidHtmlPageId))
                {
                    this.m_guidHtmlPageId = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photoalbum: title_txt
        public virtual string TitleTxt
        {
            get
            {
                return this.m_sTitleTxt;
            }
            set
            {
                if (!value.Equals(m_sTitleTxt))
                {
                    this.m_sTitleTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photoalbum: desc_txt
        public virtual string DescTxt
        {
            get { return this.m_sDescTxt; }
            set
            {
                if (!value.Equals(m_sDescTxt))
                {
                    this.m_sDescTxt = value;
                    SetDirty();
                }
            }
        }

        // tbl_photoalbum: create_dttm
        public virtual System.DateTime CreateDttm
        {
            get
            {
                return this.m_dtCreateDttm;
            }
            set
            {
                if (!value.Equals(m_dtCreateDttm))
                {
                    this.m_dtCreateDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photoalbum: update_dttm
        public virtual System.DateTime UpdateDttm
        {
            get
            {
                return this.m_dtUpdateDttm;
            }
            set
            {
                if (!value.Equals(m_dtUpdateDttm))
                {
                    this.m_dtUpdateDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photoalbum: latest_photo_id
        public virtual System.Guid LatestPhotoId
        {
            get
            {
                return this.m_guidLatestPhotoId;
            }
            set
            {
                if (!value.Equals(m_guidLatestPhotoId))
                {
                    this.m_guidLatestPhotoId = value;
                    this.SetDirty();
                }
            }
        }

        public virtual Photo[] AlbumPhotos
        {
            get
            {
                if (m_lstPhoto == null)
                {
                    m_lstPhoto = Photo.ListPhoto(PhotoalbumId);
                }
                return m_lstPhoto;
            }
        }

        public int PhotoCnt
        {
            get { return m_iPhotoCnt; }
        }

        public bool ArchiveInd
        {
            get { return m_bArchiveInd; }
            set
            {
                if (!value.Equals(m_bArchiveInd))
                {
                    m_bArchiveInd = value;
                    SetDirty();
                }
            }
        }

        public string PageUrl
        {
            get { return m_sPageUrl; }
        }

        #endregion Public properties

        public override void Load(System.Data.DataRow dr)
        {
            if (DBNull.Value.Equals(dr["photoalbum_id"]) == false)
            {
                m_guidPhotoalbumId = ((System.Guid)(dr["photoalbum_id"]));
            }
            if (!dr.IsNull("html_page_id"))
                m_guidHtmlPageId = (Guid)dr["html_page_id"];

            if (DBNull.Value.Equals(dr["title_txt"]) == false)
            {
                m_sTitleTxt = ((string)(dr["title_txt"]));
            }
            if (!dr.IsNull("desc_txt"))
                m_sDescTxt = (string)dr["desc_txt"];
            if (DBNull.Value.Equals(dr["create_dttm"]) == false)
            {
                m_dtCreateDttm = ((System.DateTime)(dr["create_dttm"]));
            }
            if (DBNull.Value.Equals(dr["update_dttm"]) == false)
            {
                m_dtUpdateDttm = ((System.DateTime)(dr["update_dttm"]));
            }
            if (DBNull.Value.Equals(dr["latest_photo_id"]) == false)
            {
                m_guidLatestPhotoId = ((System.Guid)(dr["latest_photo_id"]));
            }
            m_iPhotoCnt = (int)dr["photo_cnt"];
            m_bArchiveInd = (bool)dr["archive_ind"];
            if (dr.Table.Columns.Contains("page_url") && !dr.IsNull("page_url"))
                m_sPageUrl = dr["page_url"].ToString();
            this.SetLoaded();
        }

        public override void Save()
        {
            if (Dirty)
            {
                StoredProcArgs args = new StoredProcArgs();
                args["pPhotoalbumId"] = m_guidPhotoalbumId;
                if (m_guidHtmlPageId != Guid.Empty)
                    args["pHtmlPageId"] = m_guidHtmlPageId;
                args["pTitleTxt"] = m_sTitleTxt;
                if (!string.IsNullOrEmpty(m_sDescTxt))
                    args["pDescTxt"] = m_sDescTxt;
                if (m_guidLatestPhotoId != Guid.Empty)
                    args["pLatestPhotoId"] = m_guidLatestPhotoId;
                args["pArchiveInd"] = m_bArchiveInd;
                GenericCud.ExecuteCud("proc_cm_SavePhotoalbum", args);
                this.SetDirty(false);
            }
        }

        public static Photoalbum[] ListLatestPhotoalbum(int iPageSize)
        {
            List<Photoalbum> lst = new List<Photoalbum>();
            StoredProcArgs args = new StoredProcArgs();
            args["pPageSize"] = iPageSize;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListLatestPhotoalbums", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Photoalbum album = new Photoalbum();
                    album.Load(dr);
                    lst.Add(album);
                }
            }
            return lst.ToArray();
        }

        public static Photoalbum[] ListPhotoalbum(Guid currentHtmlPageId)
        {
            return ListPhotoalbum(currentHtmlPageId, false);
        }

        public static Photoalbum[] ListPhotoalbum(Guid currentHtmlPageId, bool bAdmin)
        {
            List<Photoalbum> lst = new List<Photoalbum>();
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = currentHtmlPageId;
            args["pAdminInd"] = bAdmin;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListPhotoalbum", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Photoalbum album = new Photoalbum();
                    album.Load(dr);
                    lst.Add(album);
                }
            }
            return lst.ToArray();
        }

        /// <summary>
        /// Get Photoalbum object
        /// </summary>
        /// <param name="guidPhotoalbumId">Guid.</param>
        /// <returns>Photoalbum.</returns>
        public static Photoalbum GetPhotoalbum(Guid guidPhotoalbumId)
        {
            Photoalbum album = new Photoalbum();
            StoredProcArgs args = new StoredProcArgs();
            args["pPhotoalbumId"] = guidPhotoalbumId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetPhotoalbum", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    album.Load(ds.Tables[0].Rows[0]);
            }
            return album;
        }
    }
}